<template>
  <VPage>
    <VTable ref="tableRef" v-bind="tableOptins">
      <template #form="{ form }">
        <XForm
          :fieldList="fieldList"
          :formOption="optionsData"
          :formData="form"
        >
          <template #create_user="{ value, input }">
            <VCascader
              :model-value="value"
              @update:model-value="input"
              collapse-tags
              class="w-[200px]"
              placeholder="创建人"
            />
          </template>
        </XForm>
      </template>
      <template #toolbar_btns>
        <VActionBar :actions="actions" />
      </template>
      <template #operation="{ row }">
        <el-button
          link
          type="primary"
          @click="handleEdit(row.id)"
          v-hasPermi="[logisticsSettingsAuthList.channel_map.edit]"
        >
          编辑
        </el-button>
        <el-button link type="primary" @click="handleLog(row)">
          日志
        </el-button>
      </template>
    </VTable>
    <!-- <DerivedCenter ref="DerivedCenterRef"></DerivedCenter> -->
    <Add
      :id="addId"
      v-model="addVisible"
      :option="optionsData"
      @close="addId = null"
      @refresh="refresh"
    />
    <SpecialLog
      dialog
      v-model="logVisible"
      :autoLoad="false"
      tag="BIND_XY_CHANNEL"
      :business_id="logId"
      @close="logId = ''"
    />
  </VPage>
</template>
<script setup lang="jsx">
import { ElMessage } from "element-plus";
import XForm from "@/components/XForm/index.vue";
import Add from "./add/index.vue";
import SpecialLog from "@/components/SpecialLog/index.vue";
// import { useEnumSelect } from "@/hooks/useEnumSelect.js";
import {
  bindXyChannelDelete,
  optsChannelWithTc,
  optsXyLsChannel,
  bindXyChannelList,
} from "./api";
// import { useFetchSelect } from "@/hooks";
import { tipMessage, getTableSelect } from "@/hooks/tool.js";
// import { getChannelList, getCountry } from "@/api/index.js";
import { logisticsSettingsAuthList } from "@/Auth/logistics-settings.js";
const { proxy } = getCurrentInstance();
const tableRef = ref();
// const params = ref({});
const tableOptins = reactive({
  formConfig: {
    data: {
      transport_type: "",
      country: "",
      time: { type: "create_time", value: [], range: false },
      user: { type: "create_user", value: [], range: false },
      status: "",
    },
  },
  tooltipConfig: {
    showAll: false,
  },
  isToggle: true,
  columns: [
    { fixed: "left", width: 50, type: "checkbox" },
    { fixed: "left", width: 50, title: "编号", type: "seq" },
    // {
    //   fixed: "left",
    //   title: "物流商",
    //   width: 100,
    //   field: "transport_type_text",
    // },
    // { title: "运输类型", minWidth: 120, field: "country_names" },
    { title: "系统渠道", minWidth: 160, field: "sys_channel_name" },

    {
      title: "跨境兴运渠道",
      minWidth: 160,
      field: "xy_ls_channel_name",
    },
    { title: "创建人", width: 170, field: "create_user" },
    { title: "创建时间", width: 170, field: "create_time" },
    { title: "更新人", width: 170, field: "update_user" },
    { title: "更新时间", width: 170, field: "update_time" },
    {
      title: "备注",
      minWidth: 200,
      field: "remark",
    },
    {
      title: "操作",
      width: 120,
      fixed: "right",
      slots: { default: "operation" },
    },
  ],
  proxyConfig: {
    autoLoad: false, // 是否自动加载查询数据
    props: {
      result: "data",
      total: "total",
    },
    ajax: {
      query: ({ page, form }) => {
        return bindXyChannelList({ ...form, ...page }).then((res) => {
          return {
            data: res.data,
            total: res.total,
          };
        });
      },
    },
  },
});
const publicStyle = { style: { width: "240px" } };
const fieldList = [
  {
    field: "sys_channel_id",
    type: "select",
    multiple: true,
    originOption: "tcChannel",
    style: { width: "200px" },
    class: "select-wrapper-style",
    options: {
      placeholder: "系统渠道",
    },
  },
  {
    field: "xy_ls_channel_id",
    type: "select",
    multiple: true,
    originOption: "xyChannel",
    style: { width: "240px" },
    class: "select-wrapper-style",
    options: {
      placeholder: "第三方渠道",
    },
  },
  {
    field: "time",
    type: "dateTimeRange",
    style: { width: "360px" },
    selectStyle: { width: "100px" },
    select: [
      { label: "创建时间", value: "create_time" },
      { label: "更新时间", value: "update_time" },
    ],
    itemProps: {
      startPlaceholder: "最早时间",
      endPlaceholder: "最晚时间",
    },
  },
  {
    field: "user",
    selectStyle: { width: "100px" },
    select: [
      { label: "创建人", value: "create_user" },
      { label: "更新人", value: "update_user" },
    ],
    options: {
      placeholder: "请选择",
    },
    slot: "create_user",
  },
];
const optionsData = ref({});
const getTcChannel = async () => {
  const { data } = await optsChannelWithTc();
  optionsData.value.tcChannel = data || [];
};
getTcChannel();
const getXyChannel = async () => {
  const { data } = await optsXyLsChannel();
  optionsData.value.xyChannel = data || [];
};
getXyChannel();
const addVisible = ref(false);
const addId = ref(null);
const handleEdit = (val) => {
  addId.value = val;
  addVisible.value = true;
};
const handleDelete = () => {
  const id = getTableSelect(tableRef.value);
  if (!id.length) return ElMessage.warning("请至少选择一条数据!");
  proxy?.confirm({
    title: "提示",
    content: (
      <p>
        确认删除选中的
        <span class="text-[#3366F1] px-[4px]">{id.length || 1}</span>
        条数据？
      </p>
    ),
    beforeClose: async (action, instance, done) => {
      if (action === "confirm") {
        try {
          instance.confirmButtonLoading = true;
          instance.confirmButtonText = "处理中..";
          const { code, message, data } = await bindXyChannelDelete({ id });
          if (code != 200) {
            ElMessage.error(message);
            return;
          }
          tipMessage(data);
        } catch (e) {
          console.error(e);
        } finally {
          instance.confirmButtonLoading = false;
          done();
          refresh();
        }
      } else {
        done();
      }
    },
  });
};
const refresh = () => {
  tableRef?.value.$table.commitProxy("query");
};

const logId = ref("");
const logVisible = ref(false);
const handleLog = (item) => {
  logId.value = item.id;
  logVisible.value = true;
};

//导出中心
// const DerivedCenterRef = ref();
// const handleExport = () => {
//   // DerivedCenterRef.value?.open({
//   //   tag_name: config_name["logistics_settings"]["logistics_manage"],
//   //   condition: params.value,
//   // });
// };
const actions = [
  {
    label: "新增",
    icon: "add2",
    type: "primary",
    fn: () => (addVisible.value = true),
    permission: [logisticsSettingsAuthList.channel_map.add],
  },
  {
    label: "删除",
    icon: "del",
    fn: handleDelete,
    permission: [logisticsSettingsAuthList.channel_map.delete],
  },
  // {
  //   label: "导入",
  //   right: true,
  //   icon: "icon-download1",
  //   fn: handleExport,
  // },
  // {
  //   label: "导出",
  //   right: true,
  //   icon: "icon-import",
  //   fn: handleExport,
  // },
];
</script>
